##
#   Project: gmediacatalog - Catalog your media to find them later
#    Author: Fabio Castelli <muflone@vbsimple.net>
# Copyright: 2010 Fabio Castelli
#   License: GPL-2+
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the Free
#  Software Foundation; either version 2 of the License, or (at your option)
#  any later version.
# 
#  This program is distributed in the hope that it will be useful, but WITHOUT
#  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
#  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
#  more details.
# 
# On Debian GNU/Linux systems, the full text of the GNU General Public License
# can be found in the file /usr/share/common-licenses/GPL-2.
##

from constants import *
from preferences import *

def get_file_size(connection, file_type, target, bytes, volume, path_uri):
  "Returns a string which represents the file size/object count"
  # Symlink, no files count
  if target:
    return 'Symlink'
  # Files count for directories
  elif file_type == ITEM_PATH and FOLDER_SIZE_COUNT:
    count = connection.files_count(volume, path_uri)
    if count >= 0:
      return '%d objects' % count
    else:
      print 'critical: folder not found %s' % path_uri
      return 'error'
  # Directory with no items count
  elif file_type == ITEM_PATH:
    return 'Directory'
  # Decode file size
  else:
    if FILE_SIZE == BYTE or (FILE_SIZE == SIZE_NATURAL and bytes < KILOBYTE):
      return '%d bytes' % (bytes)
    elif FILE_SIZE == KILOBYTE or (FILE_SIZE == SIZE_NATURAL and bytes < MEGABYTE):
      return  '%.1f kB' % (float(bytes) / KILOBYTE)
    elif FILE_SIZE == MEGABYTE or (FILE_SIZE == SIZE_NATURAL and bytes < GIGABYTE):
      return  '%.1f MB' % (float(bytes) / MEGABYTE)
    else:
      return '%.1f GB' % (float(bytes) / GIGABYTE)
  
  # Unexpected case
  return 'ERROR'
